app.controller('userorderCtrl', function ($scope, ajax, $modal, toaster, $filter) {

	var deleter = new Req.DELETE({ resource: $scope.resource });

	//订单状态
	$scope.statusList = [
		{ id: 1, name: "待付款" },
		{ id: 2, name: "支付中" },
		{ id: 3, name: "已支付" },
		{ id: 4, name: "已完成" },
		{ id: 5, name: "已取消" },
		{ id: 6, name: "订单超时" },
		{ id: 7, name: "退款中" },
		{ id: 8, name: "退款完成" },
		{ id: 9, name: "退款失败" }
	];
	$scope.imList = [
		{ id: 1, name: "电子客票" },
		{ id: 2, name: "取票码" }
	];
	$scope.agencyList = [{ id: 0, name: "未关联代售点" }];
	//模糊查询
	$scope.queryMap = {};
	$scope.timeRange = {};
	$scope.timeDeparture = {};

	$scope.reload = $scope.page.loader('order/bk/{pageIndex}/{pageSize}/page');
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.reload(1);

	$scope.checkQueryMap = function () {
		if ($scope.statusList.selected != undefined) {
			$scope.queryMap.status = $scope.statusList.selected.id;
		} else {
			delete $scope.queryMap.status;
		}
		if ($scope.imList.selected != undefined) {
			$scope.queryMap.orderDetail__immediate = ($scope.imList.selected.id == 1);
		} else {
			delete $scope.queryMap.orderDetail__immediate;
		}
		if ($scope.starList.selected != undefined) {
			// $scope.queryMap.orderDetail__startStation = $scope.starList.selected.name;
			$scope.queryMap.stationNo = $scope.starList.selected.stationNo;
		} else {
			// delete $scope.queryMap.orderDetail__startStation;
			delete $scope.queryMap.stationNo;
		}

		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
			var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
			$scope.queryMap.createTime = starTime + "#@" + endTime;
		} else {
			delete $scope.queryMap.createTime;
		}
		if ($scope.timeDeparture.startTime) {
			var dStartTime = $filter("date")($scope.timeDeparture.startTime, "yyyy-MM-dd HH:mm:ss");
			var dEndTime = $filter("date")($scope.timeDeparture.startTime, "yyyy-MM-dd HH:mm:ss");
			$scope.queryMap.orderDetail__departureTime = dStartTime + "@#" + dEndTime;
		} else {
			delete $scope.queryMap.orderDetail__departureTime;
		}

		if ($scope.agencyList.selected != undefined) {
			$scope.queryMap.orderDetail__agencyId = $scope.agencyList.selected.id;
		} else {
			delete $scope.queryMap.orderDetail__agencyId;
		}
	};

	$scope.query = function () {
		$scope.checkQueryMap();
		$scope.reload(1, $scope.queryMap);
	};

	//删除
	$scope.del = function (size, current_data, submit_type) {
		var modalInstance = $scope.delModal(size, current_data, submit_type);
		modalInstance.result.then(function (param) {
			if (param.type == 'delete') {
				deleter.id = param.data.id;
				deleter.resource = 'order/bk/' + deleter.id + '/delete';
				$scope.page.del(deleter, $scope.reload, $scope.queryMap);
			}
		});
	};

	//导出Excel
	$scope.exportExcel = function () {
		$scope.checkQueryMap();
		ajax.call(new Req.POST({
			fields: $scope.queryMap,
			resource: 'order/bk/export'
		})).then(function (result) {
			if (result.code == 200) {
				window.open(result.result);
				toaster.pop('success', '成功', '导出信息成功。');
			} else {
				toaster.pop('error', '错误', '导出信息失败！！');
			}
		}, function () { toaster.pop('error', '错误', '导出信息失败！'); });
	};

	$scope.flag = false;
	$scope.total = function () {
		$scope.checkQueryMap();
		ajax.call(new Req.GET({
			params: $scope.queryMap,
			resource: 'order/1/summary'
		})).then(function (result) {
			$scope.summary = result.result;
			$scope.totalCount = $scope.summary[0];
			$scope.orderTotal = $scope.summary[1]; //订单总额
			$scope.orderPaymentTotal = $scope.summary[2]; //订单应付金额
			$scope.insurancesToatal = $scope.summary[3]; //保险金额
			$scope.tickets = $scope.summary[4]; //票数
			$scope.im = $scope.summary[5];//电子客票金额
			$scope.imTickets = $scope.summary[6];//电子客票票数
			$scope.orderRefundToatal = $scope.summary[7];//申请退款金额
			$scope.flag = true;
		}, function () { });
	};

	//起始客运站列表读取
	ajax.get('starting/bk/list', function (result) {
		if (result && result.code == 200) $scope.starList = result.result;
	}, function () { });
	// 代售点列表
	ajax.get('agency/list', function (result) {
		if (result && result.code == 200) $scope.agencyList = $scope.agencyList.concat(result.result);
	}, function () { });

	$scope.sync = function () {
		var url = 'order/bk/sync';
		ajax.call(new Req.POST({ resource: url })).then(function (result) {
			if (result.code == 200) {
				toaster.pop('success', '成功', '同步信息成功。');
			} else {
				toaster.pop('error', '错误', '同步信息失败！');
			}
		}, function () {
			toaster.pop('error', '错误', '同步信息失败！');
		});
	};
});
